package com.joy.mms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joy.common.constant.HttpStatus;
import com.joy.common.exception.ServiceException;
import com.joy.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joy.mms.domain.bo.MachineCategoryBo;
import com.joy.mms.domain.vo.MachineCategoryVo;
import com.joy.mms.domain.MachineCategory;
import com.joy.mms.mapper.MachineCategoryMapper;
import com.joy.mms.service.MachineCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备分类Service业务层处理
 *
 * @author Joy
 * @date 2024-12-08
 */
@RequiredArgsConstructor
@Service
public class MachineCategoryServiceImpl implements MachineCategoryService {

    private final MachineCategoryMapper baseMapper;

    /**
     * 查询设备分类
     */
    @Override
    public MachineCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询设备分类列表
     */
    @Override
    public List<MachineCategoryVo> queryList(MachineCategoryBo bo) {
        LambdaQueryWrapper<MachineCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MachineCategory> buildQueryWrapper(MachineCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MachineCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryCode()), MachineCategory::getCategoryCode, bo.getCategoryCode());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), MachineCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), MachineCategory::getStatus, bo.getStatus());
        lqw.eq(bo.getParentId() != null, MachineCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getHasChild() != null, MachineCategory::getHasChild, bo.getHasChild());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), MachineCategory::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getRootId()), MachineCategory::getRootId, bo.getRootId());
        return lqw;
    }

    /**
     * 新增设备分类
     */
    @Override
    public Boolean insertByBo(MachineCategoryBo bo) {
        MachineCategory add = BeanUtil.toBean(bo, MachineCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备分类
     */
    @Override
    public Boolean updateByBo(MachineCategoryBo bo) {
        MachineCategory update = BeanUtil.toBean(bo, MachineCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MachineCategory entity){
        // 校验编码的唯一性
        String categoryCode = entity.getCategoryCode();
        LambdaQueryWrapper<MachineCategory> lqw = new LambdaQueryWrapper<>();
        lqw.in(MachineCategory::getCategoryCode,categoryCode);
        lqw.last("LIMIT 1");
        MachineCategory machineCategory = baseMapper.selectOne(lqw);
        if (machineCategory != null) {
            if (entity.getId() == null || !machineCategory.getId().equals(entity.getId())) {
                throw new ServiceException("当前分类编码已存在，请检查！",HttpStatus.WARN);
            }

        }
    }

    /**
     * 批量删除设备分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //如果有子分类则不能删除

            LambdaQueryWrapper<MachineCategory> lqw = new LambdaQueryWrapper<>();
            lqw.in(MachineCategory::getParentId,ids);
            List<MachineCategory> list = baseMapper.selectList(lqw);
           if (CollectionUtil.isNotEmpty(list)) {
                throw new ServiceException("当前分类下存在子分类，不可删除!", HttpStatus.WARN);
           }

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
